$(function () {

  function serializeObject(self){
    var o = {};
    var a = self.serializeArray();
    $.each(a, function() {
      if (o[this.name] !== undefined) {
        if (!o[this.name].push) {
          o[this.name] = [o[this.name]];
        }
        o[this.name].push(this.value || '');
      } else {
        o[this.name] = this.value || '';
      }
    });
    return o;
  }

  function tips(op) {
    var option = $.extend(op,{
      timer:2000,
      content:'amazing'
    })

    var $tips = $('#tips')
    $tips.append('<div class="tipsInner">'+option.content+'</div>')
      .addClass('')

  }


  var m = {
    init:function () {
      /* 一切基于滚动方法的动效 */
      this.scrollAnimate()

      /* bannerAnimate */
      this.bannerAnimate()
      /* menuAnimate 下划线动画 */
      this.menuAnimate()
      /* productsAnimate 产品项动画 */
      this.productsAnimate()
      /* 合作流程 hover */
      this.cooperation()
      /* footer icon animate */
      this.message()
      /* gotoTop */
      this.gotoTop()
    },
    scrollAnimate:function () {
      var _innerHeight = window.innerHeight

      /* 各个模块标题动画 */
      var pageRolling = function () {
        var topNumber = []
        var length = 0
        var index = 0
        var $partHeader = $('.partHeader')
        $partHeader.each(function (index, item) {
          topNumber.push($(item).offset().top)
          length +=1
        })
        return {
          completed:false,
          lock:false,
          run:function () {
            // console.log('enter function topNumber',topNumber)
            if(!topNumber[0]){pageRolling.completed = true}
            if(
              $(window).scrollTop()+_innerHeight -100 >
              topNumber[0]
            ) {
              if(pageRolling.lock){return false;}
              pageRolling.lock=true;
              /* 标题加载动画 */
              var $target = $partHeader.eq(index)
              var $child = $target.children()
              var subIndex = 0
              var subItemLength = $child.length

              function todo(){
                $target.children().eq(subIndex).addClass('fadeInUpFaster animated')
                if(subIndex<subItemLength){
                  setTimeout(function () {
                    subIndex+=1
                    todo()
                  },30)
                }else {
                  topNumber.shift()
                  index+=1
                  pageRolling.lock = false
                }
              }
              todo()
            }
          }
        }
      }()
      /* 选择我们 */
      var advantageAnimate =function () {
        var $customer = $('#advantage')
        var $target = $customer.find('.partCont .item')
        var length = $target.length
        var index = 0
        var topNumber = $customer.offset().top
        function todo(){
          $target.eq(index).addClass('fadeInUpFaster animated')
          if(index<length){
            setTimeout(function () {
              index+=1;
              todo()
            },80)
          }else {
            advantageAnimate.completed = true
          }
        }

        return{
          completed:false,
          run:function () {
            if($(window).scrollTop()+_innerHeight -100 > topNumber) {
              setTimeout(function () {
                todo()
              },400)
            }
          }
        }
      }()
      /* 合作流程 */
      var cooperationAnimate = function () {
        var $customer = $('#cooperation')
        var $target = $customer.find('.partCont .item')
        var length = $target.length
        var index = 0
        var topNumber = $customer.offset().top
        function todo(){
          $target.eq(index).addClass('fadeInUpFaster animated')
          if(index<length){
            setTimeout(function () {
              index+=1;
              todo()
            },120)
          }else {
            $customer.find('.startCooperation').addClass('zoomInDown animated')
            cooperationAnimate.completed = true
          }
        }

        return{
          completed:false,
          run:function () {
            if($(window).scrollTop()+_innerHeight -420 > topNumber) {
              $customer.find('.partCont .line').width('1200px')
              setTimeout(function () {
                todo()
              },800)
            }
          }
        }
      }()
      /* 关于我们模块 */
      var aboutUsAnimate = function () {
        var $aboutUs = $('#aboutUs')
        var topNumber = $aboutUs.offset().top

        return{
          completed:false,
          run:function () {
            if($(window).scrollTop()+_innerHeight -200> topNumber) {
              var $ourIntro = $aboutUs.find('.ourIntro');
              var $bgBox = $aboutUs.find('.bgBox');
              $ourIntro.css({'left':'70px','opacity':1})
              $bgBox.css({'left':'400px','opacity':1})

              setTimeout(function () {
              $ourIntro.find('p').css({'top':0,'opacity':1})
              },100)
              setTimeout(function () {
                $ourIntro.find('h3').css({'top':0,'opacity':1})
              },150)
              aboutUsAnimate.completed = true;
            }
          }
        }
      }()
      /* 合作伙伴 */
      var customerAnimate = function () {
        var $customer = $('#customer')
        var $target = $customer.find('.partCont a')
        var index = 0
        var topNumber = $customer.offset().top
        function todo(){
          $target.eq(index).addClass('slideInRight animated')
          if(index<6){
            setTimeout(function () {
              index+=1;
              todo()
            },30)
          }else {
            customerAnimate.completed = true
          }
        }

        return{
          completed:false,
          run:function () {
            if($(window).scrollTop()+_innerHeight -200 > topNumber) {
              todo()
            }
          }
        }
      }()

      function debounce(func, wait, immediate) {
        var timeout;
        return function() {
          clearTimeout(timeout);
          timeout = setTimeout(func, wait);
        };
      }
      function animateFun() {
        !pageRolling.completed && pageRolling.run()
        !advantageAnimate.completed && advantageAnimate.run()
        !cooperationAnimate.completed && cooperationAnimate.run()
        !aboutUsAnimate.completed && aboutUsAnimate.run()
        !customerAnimate.completed && customerAnimate.run()
      }
      animateFun()
      window.addEventListener('scroll',debounce(animateFun,10));
    },
    bannerAnimate:function () {
      var $banner=document.getElementById('bannerShake'),
        l = $banner.offsetWidth/2,
        t = $banner.offsetHeight/2,
        aLi=document.getElementsByTagName('span');
      $banner.onmousemove = function( ev ){
        var oEv = ev || event,
          iL = oEv.clientX,
          iT = oEv.clientY;

        for(var i=0; i<aLi.length; i++){
          aLi[i].style.marginLeft=(iL - l )/100*aLi[i].style.zIndex+'px';
          aLi[i].style.marginTop=(iT - t )/70*aLi[i].style.zIndex+'px';
        }
      }
    },
    menuAnimate:function () {
      var $navLi = $("#nav li")
      $navLi.append('<span class="after"></span>')
      $navLi
        .on('mouseover',function () {
          var $self = $(this),
            $before = $self.find('.after'),
            selfWith = $self.width()

          $before.width( selfWith )
          setTimeout(function () {
            $self.addClass('toRight')
          },500)
        })
        .on('mouseout',function () {
          var $self = $(this),
            $before = $self.find('.after')

          $before.css('width',0)
          setTimeout(function () {
            $self.removeClass('toRight')
          },500)

        })
        .find('a').on('click',function () {
          var toNumber = $($(this).data().to).offset().top
          $("html,body").animate({scrollTop:toNumber-50},500);
        })
    },
    productsAnimate:function () {
      var $products = $('#products')
      var $partCont = $products.find('.partCont')
      var $items = $partCont.find('.item')

      var option = [
        {
          bgc:'#e4f0f4'
        },
        {
          bgc:'#e5e5e3'
        },
        {
          bgc:'#f4efe8'
        }
      ]

      $items.on('mouseover',function () {
        var index = $(this).index()
        var self = $(this)
        self.find('.introDetail').show().end()
          .find('.intro').hide()
        self.siblings()
          .find('.introDetail').hide().end()
          .find('.intro').show().end().show()

        self.css({'width':'800px','background-color':option[index].bgc})
        if(index ===0){
          $items.eq(2).width(0)

        }else if(index === 1){

        }else if(index === 2){
          $items.eq(0).width(0)
        }


        // self.stop().animate({width:'800px'}, 200)
        //   .siblings().stop().animate({width:'200px'}, 200)
      })


      // $items.on('mouseout',function () {
      //   var self = $(this)
      //   self.find('.introDetail').hide().end()
      //     .find('.intro').show()
      // })
      $partCont.on('mouseout',function () {
        $items.css({'width':'400px','background-color':'transparent'})
          .find('.introDetail').hide().end()
          .find('.intro').show().end().show()
      })
    },
    cooperation:function () {
      $('#cooperation').find('.item').hover(function () {
        $(this).find('.itemInner').addClass('is-flipped')
      },function () {
        $(this).find('.itemInner').removeClass('is-flipped')
      })
    },
    message:function () {

      var $startCooperation = $('#startCooperation')
      var $feedback = $('#feedback')
      var $msg = $('#saveMsg')
      var $shadow = $('#shadow')


      $feedback.on('click',showMsgDialog)
      $startCooperation.on('click',showMsgDialog)

      function showMsgDialog() {
        $msg.find('form')[0].reset()
        $shadow.css({'display':'block','opacity':1})
        $msg.css({'display':'block','opacity':0}).removeClass('fadeOutDown').addClass('fadeInUp animated')
      }

      $msg.find('.msgSend').on('click',function () {
        $.ajax({
          url:'http://127.0.0.1:3333/saveData',
          data:serializeObject($msg.find('form')),
          type:'get',
          success:function (data) {
            layer.open({
              content: data.msg
              ,skin: 'msg'
              ,time: 2 //2秒后自动关闭
            });
            setTimeout(function () {
              $msg.find('.close').click()
            },300)
          },
          error:function (data) {
            console.log(data)
          }
        })
      })
      $msg.find('.close').on('click',function () {
        $shadow.css({'display':'none','opacity':0})
        $msg.removeClass('fadeInUp').addClass('fadeOutDown')
        setTimeout(function () {
          $msg.css({'display':'none'})
        },500)
      })
    },
    gotoTop:function () {
      $('#gotop').on('click',function () {
        $('html,body').animate({ scrollTop: 0 }, 300);
        return false;
      });
    }
  }

  m.init()

})